﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;
using CrystalDecisions.CrystalReports.Engine;

namespace ManageTicket.Ticket
{
    public partial class frmPrintTicket : Form
    {
        #region Contructor
        public frmPrintTicket(string voucher_no)
        {
            InitializeComponent();
            VOUCHER_NO = voucher_no;
        }
        #endregion

        #region Variable
        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();
        Business.Voucher boVoucher = new Business.Voucher();
        Business.Invoice boInvoice = new Business.Invoice();
        Business.Customer boCustomer = new Business.Customer();
        
        private string VOUCHER_NO = "";
        Sections crSections;
        ReportObjects crReportObjects;
        SubreportObject crSubreportObject;
        ReportDocument crSubreportDocument; 
        #endregion

        #region Event
        private void frmPrintTicket_Load(object sender, EventArgs e)
        {
            txtVoucherNo.Text = VOUCHER_NO;
            if (txtVoucherNo.Text != "")
                LoadStatus(true);
            else
                LoadStatus(false);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string Sophieu = "";
            string DatePrint = "";
            string Hotennguoimua = "";
            string Diachi = "";
            string Sotienbangchu = "";

            DataObjects.Voucher oVou = boVoucher.GetObjectByPK(txtVoucherNo.Text);
            if (oVou != null)
            {
                Sophieu = oVou != null ? oVou.VoucherNo : "";
                DatePrint = "Ngày " + oVou.DateOfVoucher.Value.Day + " Tháng " + oVou.DateOfVoucher.Value.Month + " Năm " + oVou.DateOfVoucher.Value.Year;

                DataObjects.Customer oCus = boCustomer.GetObjectByPK(oVou.CustomerId);
                if (oVou.CustomerId != "00000")
                {
                    if (oCus != null)
                    {
                        Hotennguoimua = oCus.CustomerName != null ? oCus.CustomerName : "";
                        Diachi = oCus.Address != null ? oCus.Address : "";
                    }
                }
                else
                {
                    Hotennguoimua = oVou.CustomerNameDirect;
                }
                
                //data table INVOICE               
                DataTable dtInvoice = objMethod.CreateDataTable("INVOICE", new string[] { "INVOICE_NO", "DICHVU", "SOTIENBAN", "TYGIA", "QUYDOI" });
                DataRow rowInv = dtInvoice.NewRow();
                rowInv["INVOICE_NO"] = oVou.VoucherNo;
                string content = "";
                content += "Hành trình: " + oVou.Tour + "\r\n";
                content += "Số vé: " + oVou.TicketNo;
                rowInv["DICHVU"] = content;
                rowInv["SOTIENBAN"] = oVou.SaleSum;
                rowInv["TYGIA"] = oVou.SaleExchange;
                rowInv["QUYDOI"] = oVou.SaleSumVnd;
                dtInvoice.Rows.Add(rowInv);

                Sotienbangchu = objMethod.NumberToVNsStringWithUNICODE(oVou.SaleSumVnd.ToString()) + " Đồng";
                



                if (radOfficialReceipt.Checked)
                {
                    Report.rptInvoice1 objOR = new ManageTicket.Report.rptInvoice1();
                    objOR.SetDataSource(dtInvoice);
                    FormulaFieldDefinitions crFormulas;
                    crFormulas = objOR.DataDefinition.FormulaFields;
                    crFormulas["Sophieu"].Text = "'" + Sophieu.Replace("'", "''") + "'";
                    crFormulas["DatePrint"].Text = "'" + DatePrint.Replace("'", "''") + "'";
                    //crFormulas["Sohoadon"].Text = "'" + Sohoadon.Replace("'", "''") + "'";
                    crFormulas["Hotennguoimua"].Text = "'" + Hotennguoimua.Replace("'", "''") + "'";
                    crFormulas["Diachi"].Text = "'" + Diachi.Replace("'", "''") + "'";
                    //crFormulas["Masothue"].Text = "'" + Masothue.Replace("'", "''") + "'";
                    //crFormulas["Hinhthucthanhtoan"].Text = "'" + Hinhthucthanhtoan.Replace("'", "''") + "'";
                    crFormulas["Sotienbangchu"].Text = "'" + Sotienbangchu.Replace("'", "''") + "'";                                        

                    Report.frmReport objReportOR = new ManageTicket.Report.frmReport(objOR);
                    objReportOR.ShowDialog();
                }
                else if (radOfficialReceiptVat.Checked)
                {
                    //Report.rptSaleReceipt objSR = new ManageTicket.Report.rptSaleReceipt();
                    //objSR.SetDataSource(dtVoucher);
                    //FormulaFieldDefinitions crFormulas;
                    //crFormulas = objSR.DataDefinition.FormulaFields;
                    //crFormulas["CustomerVATCode"].Text = oCustomer.TaxCode != null ? "'" + oCustomer.TaxCode.Replace("'", "''") + "'" : "' '";
                    //crFormulas["CustomerAddress"].Text = oCustomer.Address != null ? "'" + oCustomer.Address.Replace("'", "''") + "'" : "' '";
                    //crFormulas["SumSaleInWord"].Text = "'" + SumSaleInWordVND.Replace("'", "''") + "'";
                    //crFormulas["FormOfPayment"].Text = "'" + GetPayment(oVou.VoucherNo).Replace("'", "''") + "'";

                    

                    //Report.frmReport objReportSR = new ManageTicket.Report.frmReport(objSR);
                    //objReportSR.ShowDialog();
                }
            }

            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Method
        private void LoadStatus(bool bOK)
        {
            btnOK.Enabled = bOK;

            btnOK.ButtonColor = btnOK.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);

            btnOK.ForeColor = btnOK.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
        }

        private bool IsCash(string voucher_no)
        {
            Business.Invoice boInvoice = new Business.Invoice();
            ArrayList arr = boInvoice.GetListByVoucherNoAndKind(voucher_no, "VSTATUS_2");
            if (arr.Count > 0)
            {
                DataObjects.Invoice oInvoice = (DataObjects.Invoice)arr[0];
                if (oInvoice.PaymentForm == "PF_1")
                {
                    return true;
                }
            }
            return false;
        }

        private string GetEmployeeName(string empId)
        {
            Business.Employee boEmployee = new Business.Employee();
            DataObjects.Employee oEmployee = boEmployee.GetObjectByPK(empId);
            if (oEmployee != null)
                return oEmployee.LastName + " " + oEmployee.FirstName;
            return "";
        }

        private string GetPayment(string voucher_no)
        {
            Business.Invoice boInvoice = new Business.Invoice();
            Business.CodeType boCodeType = new Business.CodeType();
            ArrayList arr = boInvoice.GetListByVoucherNoAndKind(voucher_no, "VSTATUS_4");
            if (arr.Count > 0)
            {
                DataObjects.Invoice oInvoice = (DataObjects.Invoice)arr[0];
                DataObjects.CodeType oCodeType = boCodeType.GetObjectByPK(oInvoice.PaymentForm);
                if (oCodeType != null)
                {
                    return oCodeType.CodeTypeName;
                }
            }
            return "";
        }

        private string GetCodeTypeName(string Id)
        {
            Business.CodeType boCodeType = new Business.CodeType();
            DataObjects.CodeType oCodeType = boCodeType.GetObjectByPK(Id);
            if (oCodeType != null)
                return oCodeType.CodeTypeName;
            return "";
        }
        #endregion
        
    }
}
